var ttt=$("#config_location").find("option:selected").val()
var kehu='0'
var pingtai='0'

var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"

})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}
// $("#putong").attr("checked", true);
// $("#qianyue").attr("checked", true);
// 保存按钮
$("#form-member").validate({
    rules: {
        title: "titleCheck",
        color_value:"colorCheck"
    },
    //提交表单数据
    submitHandler:function(form){
        //循环class，获取到每个check的选中状态
        // if (pingtai=='0'||code=='0'){
        //     if (pingtai=='0'){
        //         $("#putong").css('outline','1px solid red')
        //         $("#qianyue").css('outline','1px solid red')
        //     }
        //     if (code=='0'){
        //         $("#beijing").css('outline','1px solid red')
        //         $("#tianjin").css('outline','1px solid red')
        //         $("#zhengzhou").css('outline','1px solid red')
        //     }
        //     layer.msg("请选择配置平台/地域？", {icon: 6, time: 3000});
        //     return false
        // }
        // if(pingtai=='10'||pingtai=='10,20'){
        //     if (kehu=='0'){
        //         $("#fen").css('outline','1px solid red')
        //         $("#nofen").css('outline','1px solid red')
        //         layer.msg("请配置签约平台下的客户类型", {icon: 6, time: 3000});
        //         return false
        //     }
        // }
        if (pingtai=='0'){
            $("#putong").css('outline','1px solid red')
            $("#qianyue").css('outline','1px solid red')
            layer.msg("请配置平台", {icon: 6, time: 3000});
            return false
        }
        if(pingtai=='10'||pingtai=='10,20'){
            if (kehu=='0'){
                $("#fen").css('outline','1px solid red')
                $("#nofen").css('outline','1px solid red')
                layer.msg("请配置签约平台下的客户类型", {icon: 6, time: 3000});
                return false
            }
        }
        if (code==''){
            var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

            $.each(warehouses.data,function(k,s){
                var warecode= document.getElementById('yun'+s.warehouseCode+'')
                $(warecode).css('outline','1px solid red')

            })
            layer.msg("请配置区域", {icon: 6, time: 3000});
            return false
        }
        var floor
        var location=$("#config_location").find("option:selected").val()
        if (location=='顶通位置'){
            floor=0
        }else if (location=='F1楼层'){
            floor=1
        }else if (location=='F2楼层'){
            floor=2
        }else if (location=='F3楼层'){
            floor=3
        }else if (location=='F4楼层'){
            floor=4
        }else if (location=='F5楼层'){
            floor=5
        }else if (location=='F6楼层'){
            floor=6
        }
        var quzhi=JSON.parse(web.readCookie("loginData"))
        var formData = new FormData();
        formData.append("different_customer_code",kehu);
        formData.append("started_at",$("#logmin").val());
        formData.append("category_id",2);
        formData.append("end_in",$("#logmax").val());
        formData.append("image_url",$("#ordinaryUrl").val());
        formData.append("ordinary_image_url",$("#url").val());
        formData.append("title",$("#title").val());
        formData.append("file",$("#file")[0].files[0]);
        formData.append("color_value",$("#color_value").val());
        formData.append("config_location",location);
        formData.append("floor",floor);
        formData.append("created_name",quzhi.user.username);
        formData.append("created_by",quzhi.user.user_ID);
        /*平台和仓库*/
        formData.append("platform",pingtai);
        formData.append("warehouse_code",code);
        formData.append("warehouse_name",CangName);
        console.log($("#file")[0].files[0])
        if ($("#config_location").find("option:selected").val()==0){
            layer.msg('请选择配置位置', {icon: 6, time: 3000});
            return false;
        }
        $.ajax({
            url: root.sroreinter.save_resource_conf,
            cache: false,  //禁用缓存
            type: "POST",
            // 告诉jQuery不要去处理发送的数据
            processData : false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType : false,
            data: formData,
            success: function (data) {
                console.log(data)
                if (data.errorCode=="92"){
                    layer.msg('您添加的开始时间小于已存在数据的结束时间!', {icon: 6, time: 3000});
                }
                if (data.errorCode=="94"){
                    layer.msg(data.errorMessage, {icon: 6, time: 3000});
                }
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    parent.queryDataFn();
                    setTimeout( function () {
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                    },700);
                }
                if (data.errorCode=="93"){
                    layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                    return
                }
            }
        });
    }
})
function changePingtai() {
    $("#putong").css('outline','')
    $("#qianyue").css('outline','')

    if ($("#qianyue").is(":checked")&&$("#putong").is(":checked")){
        $("#url").css('display','inline');
        $("#kehuleixing").css('display','inline');
        $("#ordinaryUrlXianShi").css('display','inline');
        pingtai='10'+","+'20'
    }
    if ($("#qianyue").is(":checked")&&!$("#putong").is(":checked")){
        $("#url").css('display','none');
        $("#kehuleixing").css('display','inline');
        $("#ordinaryUrlXianShi").css('display','inline');
        pingtai='10'
    }
    if ($("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#url").css('display','inline');
        $("#kehuleixing").css('display','none');
        $("#ordinaryUrlXianShi").css('display','none');
        pingtai='20'
    }
    if (!$("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#url").css('display','none');
        $("#kehuleixing").css('display','none');
        $("#ordinaryUrlXianShi").css('display','none');
        pingtai='0'
    }
}

function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        if(document.getElementById('yun'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('yun'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
            warcodename+=document.getElementById('yun'+s.warehouseCode+'').name+ "/"
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }

    code=warcode
    CangName=warcodename
}
function changeKehu() {
    $("#fen").css('outline','')
    $("#nofen").css('outline','')

    if ($("#fen").is(":checked")&&$("#nofen").is(":checked")){
        kehu='10'+","+'20'
    }
    if ($("#fen").is(":checked")&&!$("#nofen").is(":checked")){
        kehu='10'
    }
    if ($("#nofen").is(":checked")&&!$("#fen").is(":checked")){
        kehu='20'
    }
    if (!$("#nofen").is(":checked")&&!$("#fen").is(":checked")){
        kehu='0'
    }
    console.log(kehu)
}
function changImg(e){
    if ($("#config_location").find("option:selected").val()=='0'){
        layer.msg('请选择配置位置', {icon: 6, time: 2000});
        $("#file").val("")
        return false;
    }
    var image = new Image();
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            layer.msg('请上传图片!', {icon: 6, time: 2000});
            $("#file").val("")
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function(e) {
            image.src =e.target.result ;
            image.onload = function() {
                if(file.size>5*1024*1024){
                    layer.msg('图片不得大于5M!', {icon: 6, time: 2000});
                    $("#file").val("")
                    return;
                }else{
                    var element = $(e.target).next();
                    if (this.width!=1200||this.height!=80){
                        layer.msg('通栏图片大小限制为1200*80!', {icon: 6, time: 2000});
                        r=1
                        $("#file").val("")
                        return;
                    } else {
                        $("#myImg").css({width:this.width/5+"px",height:this.height/5+"px",cursor:"pointer"});
                        $("#myImg").attr("src",e.target.result);
                    }
                }
            };
        }
    }
}
//初始化客户归属下拉列表
function ascription() {
    var user_data = {}
    var succCallBack = function (data, status, response) {
        if (data.errorCode == "0") {
            var dataInter = data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
function asd(t) {
    if (t!="顶通位置"){
        $("#div").hide()
    }
    if (t=="顶通位置"){
        $("#div").show()
    }
}
$('#pic').click(function (event) {
    event.preventDefault()
    $('#file').click()
})